-- 格式要UTF8-BOM
local TWaryLua = require "TWaryLua";
local Config = require "Config";

-- 生成ServerParameter.ini
local function genServerParameter()
    local cfg = Config.game_server

    local content = ""
    content = content .. "[Correspond]\n"
    content = content .. "ConnectMax=512\n"
    content = content .. "ServicePort=6005\n"
    content = content .. "[PersonalRoom]\n"
    content = content .. "ConnectMax=512\n"
    content = content .. "ServicePort=6004\n"
    content = content .. "[ChatServer]\n"
    content = content .. "MaxPlayer=4096\n"
    content = content .. "ServicePort=6001\n"
    content = content .. "[LogonServer]\n"
    content = content .. "DelayList=1\n"
    content = content .. "ConnectMax=10240\n"
    content = content .. "ServicePort=6002\n"
    content = content .. "[GameServer]\n"
    content = content .. "BindMachineID=0\n"
    content = content .. "ServiceMachine="..  TWaryLua.XorEncrypt( TWaryLua.ServerGUID() ).."\n"  -- 系统验证码
    content = content .. "[ServerInfo]\n"
    content = content .. "ServiceName=".. TWaryLua.XorEncrypt("Server") .."\n"
    content = content .. "ServiceAddr=".. TWaryLua.XorEncrypt(cfg.ServiceAddr) .."\n" --  web公网
    content = content .. "CorrespondAddr=".. TWaryLua.XorEncrypt("127.0.0.1") .."\n" -- web内网
    content = content .. "[PlatformDB]\n"
    content = content .. "DBPort=".. cfg.SQLServerPort .."\n" -- 数据库端口
    content = content .. "DBAddr=".. TWaryLua.XorEncrypt(cfg.DBAddr) .."\n"
    content = content .. "DBUser=" .. TWaryLua.XorEncrypt(cfg.SQLServerUser) .. "\n"
    content = content .. "DBPass=" .. TWaryLua.XorEncrypt(cfg.SQLServerPass) .. "\n"
    content = content .. "[TreasureDB]\n"
    content = content .. "DBPort=".. cfg.SQLServerPort .."\n" -- 数据库端口
    content = content .. "DBAddr=".. TWaryLua.XorEncrypt(cfg.DBAddr) .."\n"
    content = content .. "DBUser=" .. TWaryLua.XorEncrypt(cfg.SQLServerUser) .. "\n"
    content = content .. "DBPass=" .. TWaryLua.XorEncrypt(cfg.SQLServerPass) .. "\n"
    content = content .. "[AccountsDB]\n"
    content = content .. "DBPort=".. cfg.SQLServerPort .."\n" -- 数据库端口
    content = content .. "DBAddr=".. TWaryLua.XorEncrypt(cfg.DBAddr) .."\n"
    content = content .. "DBUser=" .. TWaryLua.XorEncrypt(cfg.SQLServerUser) .. "\n"
    content = content .. "DBPass=" .. TWaryLua.XorEncrypt(cfg.SQLServerPass) .. "\n"
    content = content .. "[GoogleValidate]\n"
    content = content .. "GoogleSourceKey=".. TWaryLua.XorEncrypt(cfg.GooglePass).."\n"

    -- 把配置保存到两个地方
    TWaryLua.SaveToFile(cfg.Filename1, content)
    TWaryLua.SaveToFile(cfg.Filename2, content)


    -- 7.开启脚本
    for i,v in ipairs(cfg.LaunchList) do
        TWaryLua.Execute("open", v.Main, v.Param)
    end

    -- 8. 保存谷歌验证码
    TWaryLua.SaveToFile(cfg.Filename3, cfg.GooglePass)
end;


-- 修改tomcat配置
local function tomcat() 
    -- 1.加载配置
    local cfg = Config.tomcat;
   

    -- 2.打开本地配置文件，写入内存
    local file = io.open("./config/application.yaml", "r");
    local content = file:read("*all");
    file:close();


    -- 3.修改连接信息
    local newserver = "url: jdbc:sqlserver://"..cfg.SQLServerIP..":"..cfg.SQLServerPort..";DatabaseName=WHJHAccountsDB";
    local newpass = "password: "..cfg.SQLServerPass;
    local newredis = "host: "..cfg.redisIP;
    -- local newuser = "username: "..cfg.SQLServerUser;


    -- 4.将旧信息替换为新的连接信息
    content = content:gsub("url: jdbc:sqlserver://127%.0%.0%.1:1433;DatabaseName=WHJHAccountsDB", newserver);
    content = content:gsub("password: 123456", newpass);
    content = content:gsub("host: 127%.0%.0%.1", newredis);
    -- content = content:gsub("username: sa", newuser);


    -- 5.将内存中的内容替覆盖到相应的配置文件
    -- print( cfg.Filename )
    -- local file = io.open(cfg.Filename, "w");
    -- file:write(content);
    -- file:close();
    local file = TWaryLua.SaveToFile(cfg.Filename, content)


    -- 6.删除日志
    local logs = TWaryLua.GetFiles("C:\\tomcat\\logs", "*")
    for i,v in ipairs(logs) do
        TWaryLua.DeleteFile(v)
    end


    -- 7. 开启脚本    
    -- TWaryLua.Execute : 启动 "固定open", "文件名", "参数"
    TWaryLua.Execute("open", cfg.Launch.Main, cfg.Launch.Param)
end;



--修改node支付
local function node_pay()
    -- 1.加载配置
    local cfg = Config.node_pay;


    -- 2.打开本地配置文件，写入内存
    local file = io.open("./config/Define.js", "r");
    local content = file:read("*all");
    file:close();


    -- 3.修改连接信息
    local newserver = 'const server = "'..cfg.SQLServerIP..'";';
    local newpass = 'const hostMM    = "'..cfg.SQLServerPass..'";';


    -- 4.将旧信息替换为新的连接信息
    content = content:gsub('const server = "127%.0%.0%.1";', newserver)
    content = content:gsub('const hostMM%s+= "123456";', newpass)


    -- 5.将内存中的内容替覆盖到相应的配置文件
    -- local file = io.open(cfg.Filename, "w");
    -- file:write(content);
    -- file:close();
    local file = TWaryLua.SaveToFile(cfg.Filename, content)


    -- 6.删除日志
    local logdir = "C:\\Users\\Administrator\\.forever"
    local logs = TWaryLua.GetFiles(logdir, "node_2019_01*.log")
    for i,v in ipairs(logs) do
        TWaryLua.DeleteFile(v)
    end


    -- 7.开启脚本
    TWaryLua.Execute("open", cfg.Launch.Main, cfg.Launch.Param)
end



--修改node登录
local function node_login()
    -- 1.加载配置文件
    local cfg = Config.node_login;


    -- 2.打开本地配置文件，写入内存
    local file1 = io.open("./config/config.js", "r");      -- 修改数据库连接配置文件
    local content1 = file1:read("*all");
    file1:close();

    local file2 = io.open("./config/GameList.json", "r");   -- 修改gamelist文件
    local content2 = file2:read("*all");
    file2:close();


    -- 3.修改连接信息
    local newserver = "host: '"..cfg.SQLServerIP.."'";
    local newpass = "pass: '"..cfg.SQLServerPass.."'";
    local newurl = '"downloadurl": "'..cfg.DownloadUrl..'",';
    -- local newport = "port: "..cfg.SQLServerPort;
    -- local newuser = "user: '"..cfg.SQLServerUser.."'";


    -- 4.将旧信息替换为新的连接信息
    content1 = content1:gsub("host: '127%.0%.0%.1'", newserver);
    content1 = content1:gsub("pass: '123456'", newpass);
    -- content1 = content1:gsub("port: 1433", newport);
    -- content1 = content1:gsub("user: 'node_login_app'", newuser);

    content2 = content2:gsub('"downloadurl": "http://www.baidu.com",', newurl);


    -- 5.将内存中的内容替覆盖到相应的配置文件
    -- local file1 = io.open(cfg.Filename1, "w");
    -- file1:write(content1);
    -- file1:close();
    local file1 = TWaryLua.SaveToFile(cfg.Filename1, content1)

    -- local file2 = io.open(cfg.Filename2, "w");
    -- file2:write(content2);
    -- file2:close();
    local file2 = TWaryLua.SaveToFile(cfg.Filename2, content2)


    -- 6.删除日志
    local logs = TWaryLua.GetFiles("C:\\NoticeServer[2019-07-16]\\logger\\log", "*log")
    for i,v in ipairs(logs) do
        TWaryLua.DeleteFile(v)
    end


    -- 7.开启脚本
    TWaryLua.Execute("open", cfg.Launch.Main, cfg.Launch.Param);

end



--修改iis
local function iis()
    -- 1.加载配置文件
    local cfg = Config.iis_app;


    -- 2.打开本地配置文件，写入内存
    local file = io.open("./config/Web.config", "r");
    local content = file:read("*all");
    file:close();


    -- 3.修改连接信息
    local newserver = "Data Source="..cfg.SQLServerIP..";";
    local newpass = "Password="..cfg.SQLServerPass..";";
    -- local newuser = "User ID="..cfg.SQLServerUser;


    -- 4.将旧信息替换为新的连接信息
    content = content:gsub("Data Source=127%.0%.0%.1;", newserver);
    content = content:gsub("Password=123456;", newpass);


    -- 5.将内存中的内容替覆盖到相应的配置文件
    -- local file = io.open(cfg.Filename, "w");
    -- file:write(content);
    -- file:close();
    local file = TWaryLua.SaveToFile(cfg.Filename, content)


    -- 6.开启脚本
    TWaryLua.Execute("open", cfg.Launch.Main, cfg.Launch.Param)
end



--修改新聊天服
local function new_chat()
    -- 1.加载配置文件
    local cfg = Config.new_chat;


    -- 2.打开本地配置文件，写入内存
    local file = io.open("./config/ConfigInfo.xml", "r");
    local content = file:read("*all");
    file:close();


    -- 3.修改连接信息
    local newserver = 'szDBAddress="'..cfg.SQLServerIP..'"';
    local newport = 'wDBPort="'..cfg.SQLServerPort..'"';
    local newuser = 'szDBUser="'..cfg.SQLServerUser..'"';
    local newpass = 'szDBPassword="'..cfg.SQLServerPass..'"';


    -- 4.将旧信息替换为新的连接信息
    content = content:gsub('szDBAddress="127%.0%.0%.1"', newserver);
    content = content:gsub('szDBPassword="123456"', newpass);
    -- content = content:gsub('wDBPort="1433"', newport);
    -- content = content:gsub('szDBUser="java_app"', newuser);


    -- 5.将内存中的内容替覆盖到相应的配置文件
    -- local file = io.open(cfg.Filename, "w");
    -- file:write(content);
    -- file:close();
    local file = TWaryLua.SaveToFile(cfg.Filename, content)


    -- 6.删除日志
    local logs = TWaryLua.GetFiles("C:\\Users\\Administrator\\Desktop\\聊天服\\log", "*log")
    local logs1 = TWaryLua.GetFiles("C:\\Users\\Administrator\\Desktop\\聊天服\\ChatProxyServer\\logger\\log", "*log")
    --将两个日志放在一个表内
    for i,v in ipairs(logs1) do
        table.insert(logs, v)
    end
    for i,v in ipairs(logs) do
        TWaryLua.DeleteFile(v)
    end


    -- 7.开启脚本
    for i,v in ipairs(cfg.LaunchList) do
        TWaryLua.Execute("open", v.Main, v.Param)
    end
end


genServerParameter()
node_pay()
node_login()
-- tomcat()
-- iis()
new_chat()


